<?php
namespace app\controllers;

use app\common\corelib\Excel;
use yii;
use app\common\logic\AdminLogic;


/**
 * 控制器基类，会检查是否登录
 */
class BackendController extends BaseController
{

    public $exceptAuthors = ['index', 'login'];
    public $exceptActions = ['getzone'];


    public function beforeAction($action)
    {
        $this->enableCsrfValidation = false;

        $controllerID = Yii::$app->controller->id;
        $actionID = Yii::$app->controller->action->id;
        /*$nologin=Yii::$app->params['nologin'];
        if(!isset($nologin[$controllerID]) && in_array($actionID,$nologin[$controllerID]))*/
            $this->_checkLogin();
        //$this->_checkPrivilege($action);
        $this->layout = '../layout/main';
        unset($_GET['menu_id']);
        unset($_GET['s']);
        return parent::beforeAction($action);
    }

    /**
     * 检查是否登录
     */
    public function _checkLogin()
    {
        $userinfo = \yii::$app->session->get('user');
        $uid = $userinfo['uid'];
        $role = $userinfo['role'];
        if ($uid > 0 && $role == 'PADMIN') {
            $username = $userinfo['username'];
            $this->data['header_username'] = $username;
            return true;
        } else {
            \yii::$app->session->remove('user');
            jump('login/index');
        }

    }

    /**
     * 检查权限
     */
    public function _checkPrivilege($action)
    {
        if(in_array($action->id, $this->exceptActions))
            return true;
        $result = false;
        $userinfo = \yii::$app->session->get('user');
        $adminId = $userinfo['uid'];

        if ($userinfo['username'] == 'Adminstor') {
            return true;
        }
        if ($adminId > 0) {
            $adminlogic = new AdminLogic();
            $adminAuthorIds = $adminlogic->getAdminAuthorIds($adminId);
            if (!empty($adminAuthorIds)) {
                $action = strtolower($this->id . '/' . $action->id);
                $adminAuthor = $adminlogic->getAdminAuthorByAction($action);
                if (abs($adminAuthor['id']) > 0 && in_array($adminAuthor['id'], explode(',', $adminAuthorIds))) {
                    $result = true;
                }
            }
        }

        if (!$result && !in_array($this->id, $this->exceptAuthors)) {
            jump(['index/error']);
        }
        return $result;
    }


    /**
     * 后台统一返回json格式
     * @param integer $code 错误代码
     * @param string $msg 错误信息
     * @param array $data 返回数据
     * @return array
     */
    public function jsonResponse($code=0, $msg='', $data=[])
    {
        $response = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];

        return json_encode($response);
    }

    public function checkParams($array = [])
    {
        if (!empty($array)) {
            foreach ($array as $value) {
                if (!isset($this->params[$value]) || $this->params[$value] === '') return false;
            }
        }
        return true;
    }
}